<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\first\model;

use think\Model;

class Member extends Model{
    protected $pk = 'id';
    protected $auto = ['last_login_ip','update_time'];
    protected $insert = ['sex' => 0];
    protected $autoWriteTimestamp = true;
    protected  static $_field = ['id as user_id,username,information,sex,nickname,head,tel,
        hobbies,email,region,address,rong_token'];

    /**
     * 获取用户
     * @param $data
     * @return array|null|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getMember($data){
        $info =  is_array($data)?self::field(self::$_field)
            ->where($data)->find():self::field(self::$_field)->find($data);
        if($info){
            $collection = new Collection();
            $subscribe = new Subscribe();
            $fans = new Fans();
            $info['subscribe'] = $subscribe->where('mid','eq',$data)->count();
            $info['collection'] = $collection->where('mid','eq',$data)->count();
            $info['fans'] = $fans->where('mid','eq',$data)->count();
        }
        return $info;
    }

    public static function issub($id,$rid){
        $fans = new Fans();
        $info = $fans
            ->where('rid','eq', $id)
            ->where('mid','eq',$rid)
            ->count();
        return $info?1:0;
    }

    public function getSexAttr($value)
    {
        $sex = [0=>'未知',1=>'男',2=>'女'];
        return $sex[$value];
    }

    public function getHeadAttr($value){
        if(empty($value)){
            return null;
        }else if(strpos($value,'://')===false){
            return request()->Domain().DIRECTORY_SEPARATOR.substr($value,1);
        }else{
            return $value;
        }
    }

    protected function setLastLoginIpAttr()
    {
        return request()->ip();
    }

    protected function setUpdateTimeAttr(){
        return time();
    }
}